<template>
	<view class="container">
		<view class="grade_box">
			<block v-for="(item,index) in grade_list" :index="index" :key="item.xd_code">
				<view class="grade_title_box">
					<text style=" margin: 40rpx 5rpx;   ">{{item.xd_name}}</text>
				</view>

				<uni-grid :column="2" :showBorder="false" :highlight="false" :square="false">
					<uni-grid-item v-for="(grade,index_1)  in item.list " :index="index_1" :key="grade.stage_code">
						<view :class="grade.grade_code==grade_info.grade_code?'grid-item-box-checked':'grid-item-box'"
							@click="change(index,index_1)">
							<text class="grid_item_text">{{grade.grade_name}}</text>
						</view>
					</uni-grid-item>
				</uni-grid>

			</block>
		</view>
	</view>
</template>

<script>
	import api from './grade.js'
	export default {
		data() {
			return {
				grade_info: {},
				grade_list: [],
			}
		},
		onReady() {
			this.getCacheGradeList();
		},
		onLoad(option) {
			let gradeJson = decodeURIComponent(option.grade);
			this.grade_info = JSON.parse(gradeJson);
			uni.hideShareMenu( );
		},
		methods: {
			back() {
				uni.navigateBack({
					delta: 1
				})
			},
			change(index, index_1) {
				this.grade_info = this.grade_list[index].list[index_1];
				uni.$emit('updateGradeData',encodeURIComponent(JSON.stringify(this.grade_info)))
				uni.navigateBack({
					delta: 1
				})
			},
			//取年级及缓存;
			getCacheGradeList(){
							 let that=this;
							 uni.getStorage({
								key: 'cache_grade_list',
								success(res) {
									that.grade_list = res.data;
									//console.log(that.grade_list);
									if(null==that.grade_list || that.grade_list.length==0){
										 that.getGradeList();
									}
								},
								fail() {
									 that.getGradeList();
								}
							 })
			},
			async getGradeList() {
				let that=this;
				let res = await api.getGradeList();
				that.grade_list = res.data.data;
				//写缓存;
				uni.setStorage({
					key: 'cache_grade_list',
					data: that.grade_list,
					success: function() {}
				});
			},
		},

	}
</script>

<style lang="scss">
	@import "./grade.scss";
</style>
